package com.lhb.aicodemother.controller;

import cn.hutool.core.util.ObjUtil;
import com.lhb.aicodemother.annotation.AuthCheck;
import com.lhb.aicodemother.common.BaseResponse;
import com.lhb.aicodemother.common.ResultUtils;
import com.lhb.aicodemother.constant.UserConstant;
import com.lhb.aicodemother.exception.BusinessException;
import com.lhb.aicodemother.exception.ErrorCode;
import com.lhb.aicodemother.exception.ThrowUtils;
import com.lhb.aicodemother.model.dto.chathistory.ChatHistoryQueryRequest;
import com.lhb.aicodemother.model.entity.ChatHistory;
import com.lhb.aicodemother.model.entity.User;
import com.lhb.aicodemother.service.ChatHistoryService;
import com.lhb.aicodemother.service.UserService;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.*;


import java.time.LocalDateTime;

/**
 * 对话历史 控制层。
 *
 * @author lihai
 */
@RestController
@RequestMapping("/chatHistory")
public class ChatHistoryController {

    @Resource
    private ChatHistoryService chatHistoryService;

    @Resource
    private UserService userService;

    /*
     * 分页查询 --游标查询
     * */
    @GetMapping("/app/{appId}")
    public BaseResponse<Page<ChatHistory>> listAppChatHistory(@PathVariable Long appId, @RequestParam(defaultValue = "10") int pageSize,
                                                              @RequestParam(required = false) LocalDateTime lastCreateTime,
                                                              HttpServletRequest request) {
        User loginUser = userService.getLoginUser(request);
        Page<ChatHistory> chatHistoryPage = chatHistoryService.listAppChatHistoryByPage(appId, pageSize, lastCreateTime, loginUser);
        return ResultUtils.success(chatHistoryPage);
    }

    /*
    * 管理员查询所有的对话历史
    * */
    @PostMapping("/admin/list/page/vo")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Page<ChatHistory>> listAllChatHistoryByPageForAdmin(@RequestBody ChatHistoryQueryRequest chatHistoryQueryRequest){
        ThrowUtils.throwIf(chatHistoryQueryRequest == null, ErrorCode.PARAMS_ERROR);
        int pageNum = chatHistoryQueryRequest.getPageNum();
        int pageSize = chatHistoryQueryRequest.getPageSize();
        QueryWrapper queryWrapper = chatHistoryService.getQueryWrapper(chatHistoryQueryRequest);
        Page<ChatHistory> result = chatHistoryService.page(Page.of(pageNum, pageSize), queryWrapper);
        return ResultUtils.success(result);
    }

    /*
    * 删除历史对话 根据对话id
    * */
    @DeleteMapping("/delete/{id}")
    public BaseResponse<Boolean> deleteChatHistoryById(@PathVariable Long id,HttpServletRequest request){
        ThrowUtils.throwIf(id == null || id < 0, ErrorCode.PARAMS_ERROR);
        ChatHistory chatHistory = chatHistoryService.getById(id);
        ThrowUtils.throwIf(ObjUtil.isEmpty(chatHistory),ErrorCode.PARAMS_ERROR,"ID不存在");
        User loginUser = userService.getLoginUser(request);
        // 只有本人能删除历史对话
        if(!loginUser.getId().equals(chatHistory.getUserId())){
            throw new BusinessException(ErrorCode.NO_AUTH_ERROR);
        }
        boolean byId = chatHistoryService.removeById(id);
        return ResultUtils.success(byId);
    }



}
